package xyz.paragon.review.event;

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
import xyz.paragon.review.persistence.service.ReviewService;

import javax.transaction.Transactional;

@Component
@Transactional
@EnableBinding({EventProcessor.class})
public class EventChecker {

    private final ReviewService reviewService;

    public EventChecker(ReviewService reviewService) {
        this.reviewService = reviewService;
    }

    @StreamListener(target = EventProcessor.EVENTS_IN)
    public void processDeleteProduct(DeleteProductEvent deleteProductEvent) {
        this.reviewService.removeReviewsByProductId(deleteProductEvent.getProductId());
        System.out.println("Event: " + deleteProductEvent);
    }

}
